package com.luoxue.security;

import com.luoxue.entity.User;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.time.Instant;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * JWT 工具类
 */
@Component
public class JwtTokenUtil implements Serializable {

    public Map<String, UserDetails> userDetailsMap = new ConcurrentHashMap<>();

//    private static final String CLAIM_KEY_USERNAME = "sub";

    // 1天(毫秒)
    private static final long EXPIRATION_TIME = 1000 * 60 * 60 * 24;

    // JWT密码
    private static final String SECRET = "secret";


    public void addUser(UserDetails userDetails) {
        this.userDetailsMap.put(userDetails.getUsername(), userDetails);
    }

    public UserDetails getUser(String username) {
        return this.userDetailsMap.get(username);
    }

    public void removeUser(String username) {
        this.userDetailsMap.remove(username);
    }


    /**
     * 签发JWT
     */
    public String generateToken(UserDetails userDetails) {
//        Map<String, Object> claims = new HashMap<>(16);
//        claims.put(CLAIM_KEY_USERNAME, userDetails.getUsername());
        return Jwts.builder()
                .setSubject(userDetails.getUsername())
//                .setClaims(claims)
                .setExpiration(new Date(Instant.now().toEpochMilli() + EXPIRATION_TIME))
                .signWith(SignatureAlgorithm.HS512, SECRET)
                .compact();
    }

    /**
     * 验证JWT
     */
    public Boolean validateToken(String token, UserDetails userDetails) {
        User user = (User) userDetails;
        String username = getUsernameFromToken(token);
        return (username.equals(user.getUsername()) && !isTokenExpired(token));
    }

    /**
     * 获取token是否过期
     */
    public Boolean isTokenExpired(String token) {
        Date expiration = getExpirationDateFromToken(token);
        return expiration.before(new Date());
    }

    /**
     * 根据token获取username
     */
    public String getUsernameFromToken(String token) {
        return getClaimsFromToken(token).getSubject();
    }

    /**
     * 获取token的过期时间
     */
    public Date getExpirationDateFromToken(String token) {
        return getClaimsFromToken(token).getExpiration();
    }

    /**
     * 解析JWT
     */
    private Claims getClaimsFromToken(String token) {
        return Jwts.parser()
                .setSigningKey(SECRET)
                .parseClaimsJws(token)
                .getBody();
    }


}
